<template>
  <view class="security-center">
    <!-- 导航栏 -->
    <Header title="安全中心"></Header>
    <!-- 背景-->
    <view class="bg"></view>

    <!-- 卡片-->
    <view class="card">
      <view class="item" @click="isChangePwd=true">
        <view>修改密码</view>
        <uni-icons type="forward" size="15"></uni-icons>
      </view>
      <view class="item" @click="destroyAccount">
        <view>注销账号</view>
        <uni-icons type="forward" size="15"></uni-icons>
      </view>
    </view>

    <!-- 退出登录 -->
    <view class="sign-out">
      <view class="btn" @click="logout">
        退出登录
      </view>
    </view>


    <!-- 修改密码 -->
    <view class="change_pwd" :class="isChangePwd?'show':''">
      <view class="header">
        <text>修改密码</text>
        <uni-icons type="closeempty" size="20" @click="isChangePwd=false"></uni-icons>
      </view>
      <!-- 表单-->
      <view class="form">
        <view class="form-item">
          <label for="">旧密码</label>
          <input v-model="pwd.oldPassword" :password="!eye.old" placeholder="旧密码(首字符必须为字母)">
          <uni-icons @click="eyeHandler('old')" :type="eye.old?'eye':'eye-slash'" size="20" color="#b1afaf">
          </uni-icons>
        </view>
        <view class="form-item">
          <label for="">新密码</label>
          <input v-model="pwd.password" :password="!eye.new" placeholder="新密码(首字符必须为字母)">
          <uni-icons @click="eyeHandler('new')" :type="eye.new?'eye':'eye-slash'" size="20" color="#b1afaf">
          </uni-icons>
        </view>
      </view>

      <!-- 提交 -->
      <view class="submit" @click="updatePassword">
        提交
      </view>
    </view>

    <!-- 遮罩层 -->
    <view v-show="isChangePwd" class="isChangePwd" @click="isChangePwd = false"></view>
  </view>
</template>

<script>
  import Header from "../../../../components/HeaderNav/HeaderNav.vue"
  import {
    req
  } from "../../../utils/request/request";
  import {
    uniToast
  } from "../../../utils/uni/uni";
  import {
    mapActions
  } from 'vuex'
  import {
    validForm
  } from "../../../utils/validForm";
  export default {
    components: {
      Header
    },
    data() {
      return {
        isChangePwd: false,
        eye: {
          old: false,
          new: false
        },
        pwd: {
          password: "",
          oldPassword: ""
        }
      };
    },
    methods: {
      ...mapActions("user", ['logout', 'destroyData']),

      /* 修改密码 */
      updatePassword() {
        let o = {
          oldPassword: {
            value: this.pwd.oldPassword,
            errorMsg: '旧密码由数字字母下划线组合(6-16字符)',
            reg: /^[A-Za-z]\w{5,15}$/
          },
          password: {
            value: this.pwd.password,
            errorMsg: '新密码由数字字母下划线组合(6-16字符)',
            reg: /^[A-Za-z]\w{5,15}$/
          }
        };
        const isPass = validForm.valid(o)
        if (!isPass) return
        req('updatePassword', {
          reqData: this.pwd,
          method: 'POST',
          isToken: true
        }).then(({
          data: res
        }) => {
          if (res['code'] === 'E001') {
            uni.showModal({
              title: res['msg'],
              content: '请重新登录',
              success: (res) => {
                res.cancel && this.goIndex()
                res.confirm && this.goLogin()
              }
            })
          }
          uniToast(res['msg'])
        }, err => {})
      },
      goIndex() {
        uni.switchTab({
          url: '/pages/index/index',
          success: () => {
            this.destroyData()
          }
        })
      },
      goLogin() {
        uni.navigateTo({
          url: '/pages/login/login',
          success: () => {
            this.destroyData()
          }
        })
      },
      /* 注销账号*/
      destroyAccount() {
        uni.showModal({
          title: "注销账号",
          content: '是否确定注销账号，一旦注销无法恢复！',
          success(res) {
            if (res.confirm) {
              req('destroyAccount', {
                isToken: true,
                method: 'POST'
              }).then(res => {
                uniToast(res['msg'])
                this.timer = setTimeout(() => {
                  uni.switchTab({
                    url: '/pages/index/index'
                  })
                }, 500)
              }, err => {})
            }
          }
        })
      },
      eyeHandler(eye) {
        this.$set(this.eye, eye, !this.eye[eye])
      },
      gotoAddAddressHandler() {
        uni.navigateTo({
          url: '/pages/my/my/my_address/add_address/add_address'
        })
      },
      editAddressHandler() {
        uni.navigateTo({
          url: '/pages/my/my/my_address/add_address/add_address?title=编辑地址',
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  page {
    background-color: #f7f7f7;
    position: relative;
    height: 100vh;
  }

  .security-center {
    overflow: hidden;

    .isChangePwd {
      position: absolute;
      background-color: #0000008c;
      height: 100%;
      top: 0;
      left: 0;
      right: 0;
    }

    .bg {
      background-color: #0c34ba;
      height: 200rpx;
    }

    .card {
      display: flex;
      border-top-right-radius: 15rpx;
      border-top-left-radius: 15rpx;
      overflow: hidden;
      background-color: #fff;
      transform: translateY(-50rpx);
      margin: 0 30rpx;
      padding: 30rpx;
      flex-direction: column;

      .item {
        font-size: 24rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 70rpx;
        border-bottom: #f7f7f7 solid 2rpx;

        &:last-child {
          border-bottom: #f7f7f7 solid 0;
        }
      }
    }


    .sign-out {
      padding: 30rpx;
      margin-top: 30%;

      .btn {
        background-color: #0c34ba;
        color: #fff;
        border-radius: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 26rpx;
        height: 80rpx;
      }
    }

    .change_pwd {
      background-color: #fff;
      padding: 30rpx;
      font-size: 28rpx;
      position: absolute;
      left: 0;
      right: 0;
      bottom: -100%;
      z-index: 1;
      transition: all .3s linear;

      &.show {
        bottom: 0;
      }

      .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 80rpx;
      }

      .form {
        .form-item {
          display: flex;
          height: 80rpx;
          align-items: center;
          padding: 0 40rpx;

          input {
            flex: 1;
            font-size: 24rpx;
          }

          label {
            font-size: 24rpx;
            width: 150rpx;
          }
        }
      }

      .submit {
        background-color: #0c34ba;
        color: #fff;
        border-radius: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 70rpx;
        margin: 0 30rpx;
        margin-top: 80rpx;
        font-size: 24rpx;
      }



    }

  }
</style>
